package com.zzc.emotisenseclassroom.domain.user.service.impl;

import com.zzc.emotisenseclassroom.components.RandomValidateCode;
import com.zzc.emotisenseclassroom.domain.user.service.VerificationCodeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright Zzc
 * FileName: VerificationCodeServiceImpl
 * Author:   Zzc
 * Date:     2024/4/1523:52
 * Description:验证码业务层实现类
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 * Zzc                   1.0.0
 */
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {
    private static final Logger LOG = LogManager.getLogger(VerificationCodeServiceImpl.class);
    public static final String ZZC_RANDOM_VALIDATE_CODE = "RandomValidateCode";// 放到session中的key

    @Autowired
    RandomValidateCode randomValidateCode;



    @Override
    public void generateValidateCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            LOG.error("生成验证码异常", e);
        }
    }

    @Override
    public boolean rightValidateCode(HttpServletRequest httpServletRequest, String validateCode) {
        HttpSession session = httpServletRequest.getSession();
        String attribute = (String)session.getAttribute(ZZC_RANDOM_VALIDATE_CODE);

        if (StringUtils.isBlank(validateCode)){
            return false;
        }

        return validateCode.equalsIgnoreCase(attribute);
    }
}
